home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / texvol / draw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  24.0 KB  |  1,001 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------------------
  18.  *
  19.  * file   : draw.c 
  20.  *
  21.  * Author : Yusuf Attarwala
  22.  * Date   : May 93
  23.  *
  24.  *---------------------------------------------------------------------------*/
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <gl/device.h>
  28.  
  29. #include "globals_vol.h"
  30.  
  31. #define ABS(a)   (((a) >= 0) ? (a) : -(a))
  32. #define _XY    1
  33. #define _YZ    2
  34. #define _ZX    3
  35. #define _MXY  -1
  36. #define _MYZ  -2
  37. #define _MZX  -3
  38.  
  39. void drawAxes(),
  40.      drawClippingPlane();
  41. int isFacing();
  42. int optimumSet();
  43. static int lastSet = 0;
  44. static int visitedDrawscene = 0;
  45.  
  46. static float t0[] = {0.0,0.0};
  47. static float t1[] = {1.0,0.0};
  48. static float t2[] = {1.0,1.0};
  49. static float t3[] = {0.0,1.0};
  50.  
  51. static float xyNorm[]  = {0.0,0.0,1.0};
  52. static float mxyNorm[] = {0.0,0.0,-1.0};
  53. static float zxNorm[]  = {0.0,1.0,0.0};
  54. static float mzxNorm[] = {0.0,-1.0,0.0};
  55. static float yzNorm[]  = {1.0,0.0,0.0};
  56. static float myzNorm[] = {-1.0,0.0,0.0};
  57.  
  58. float x_sep = 1.0;
  59. float y_sep = 1.0;
  60. float z_sep = 1.0;
  61.  
  62. void xt3f(){}
  63.  
  64. void
  65. drawScene()
  66. {
  67.     int i,j;
  68.     int viewAxis;
  69.     float tcoord[4][3];
  70.     int skipPolyCounter;
  71.  
  72. /*printf("drawscene \n");*/
  73.  
  74.     /* the following is so that 2d texture is bound 
  75.        first few times, regardless of lastSet = viewAxis */
  76.     visitedDrawscene++;
  77.     if (visitedDrawscene > 100) visitedDrawscene = 10;
  78.  
  79.     GLXwinset(XtDisplay(glw), XtWindow(glw));
  80.  
  81.     if (fog) {
  82.     c3f(fogRGB);
  83.     fogvertex(FG_ON,0);
  84.     }
  85.     else {
  86.         RGBcolor(0,0,0);
  87.     }
  88.     clear();
  89.  
  90.     /* enable arbitrary clipping planes */
  91.     if (doClipping) {
  92.         for (i=0;i<6;i++) {
  93.             if (clip[i].onOff == 1) {
  94.                 clipplane(i,CP_DEFINE,clip[i].coeff);
  95.                 clipplane(i,CP_ON,0);
  96.             }
  97.         }
  98.     }
  99.  
  100.     viewAxis = optimumSet();
  101.  
  102.     switch(drawMode) {
  103.     case WIREFRAME :
  104.         pushmatrix();
  105.     switch(viewAxis) {
  106.     case _XY :
  107.         case _MXY :
  108.         /*
  109.         translate(0,0,-z_sep*depth/2.0);
  110.             for (i=0;i<depth;i++) {
  111.             translate(0,0,z_sep);
  112.         */
  113.                 RGBcolor(255,0,0);
  114.                 bgnclosedline();
  115.                 v3f(xy_v0);
  116.                 v3f(xy_v1);
  117.                 v3f(xy_v2);
  118.                 v3f(xy_v3);
  119.                 endclosedline();
  120.         /*
  121.             }
  122.         */
  123.         break;
  124.     case _YZ :
  125.         case _MYZ :
  126.         /*
  127.         translate(-x_sep*width/2.0,0,0);
  128.             for (i=0;i<width;i++) {
  129.             translate(x_sep,0,0);
  130.         */
  131.                 RGBcolor(0,255,0);
  132.                 bgnclosedline();
  133.                 v3f(yz_v0);
  134.                 v3f(yz_v1);
  135.                 v3f(yz_v2);
  136.                 v3f(yz_v3);
  137.                 endclosedline();
  138.         /*
  139.             }
  140.         */
  141.         break;
  142.     case _ZX :
  143.         case _MZX :
  144.         /*
  145.         translate(0,-y_sep*height/2.0,0);
  146.             for (i=0;i<height;i++) {
  147.             translate(0,y_sep,0);
  148.         */
  149.                 RGBcolor(0,0,255);
  150.                 bgnclosedline();
  151.                 v3f(zx_v0);
  152.                 v3f(zx_v1);
  153.                 v3f(zx_v2);
  154.                 v3f(zx_v3);
  155.                 endclosedline();
  156.         /*
  157.             }
  158.         */
  159.         break;
  160.     default :
  161.         printf("weird view \n");
  162.         break;
  163.     }
  164.         popmatrix();
  165.         if (drawAxis) drawAxes();
  166.  
  167.         if (doClipping && displayClipPlanes) {
  168.             for (i=0;i<6;i++) {
  169.                 if (clip[i].onOff==1) {
  170.                     drawClippingPlane(i);
  171.                 }
  172.             }
  173.         }
  174.     break;
  175.  
  176.     /* the real thing */
  177.     case TEXTURED :
  178.  
  179.         if (doLighting) {
  180.             lmbind(LMODEL,1);      /* bind light model        */
  181.             pushmatrix();
  182.             lmbind(LIGHT0,1);
  183.             popmatrix();
  184.             lmbind(MATERIAL,1);    /* bind material           */
  185.             shademodel(FLAT);
  186.         }
  187.  
  188.     if (!dontBlend)
  189.         blendfunction( BF_SA, BF_MSA );
  190.  
  191.         pushmatrix();
  192.  
  193.         if (textureMode == TEXTURE_2D) {       /* TWO DIM */
  194.  
  195.             tevbind(TV_ENV0,1);
  196.         switch(viewAxis) {
  197.         case _XY :
  198.         skipPolyCounter = 0;
  199.         translate(0,0,z_sep*depth/2.0);
  200. #ifndef OPTI
  201. if (lastSet != viewAxis || visitedDrawscene < 5) texbind(TX_TEXTURE_0,100);
  202. #endif
  203.         for (i=0;i<depth;i++) {
  204.                     /* if (degenMode && qtest()) break; */
  205.             translate(0,0,-z_sep);
  206.  
  207.                     /* weird logic to skip polygons */
  208.             if (lightWeight) {
  209.                 if (skipPolyCounter < lightWeightCount) {
  210.                 skipPolyCounter++;
  211.                 continue;
  212.             }
  213.             else {
  214.                 skipPolyCounter = 0;
  215.             }
  216.                     } 
  217.  
  218. #ifdef OPTI
  219.             texbind(TX_TEXTURE_0,i+1);
  220. #endif
  221.             cpack(cpackValue);
  222.             if (doLighting) n3f(xyNorm);
  223.             bgnpolygon();
  224. #ifndef OPTI
  225. t0[1] = t1[1] = (float)i/(float)depth;
  226. t2[1] = t3[1] = (float)(i+1)/(float)depth;
  227. #endif
  228.             t2f(t0);
  229.             v3f(xy_v0);
  230.             t2f(t1);
  231.             v3f(xy_v1);
  232.             t2f(t2);
  233.             v3f(xy_v2);
  234.             t2f(t3);
  235.             v3f(xy_v3);
  236.             endpolygon();
  237.         }
  238.         break;
  239.         case _YZ :
  240.         skipPolyCounter = 0;
  241.         translate(x_sep*width/2.0,0,0);
  242. #ifndef OPTI
  243. if (lastSet != viewAxis) texbind(TX_TEXTURE_0,400);
  244. #endif
  245.         for (i=0;i<width;i++) {
  246.                     /* if (degenMode && qtest()) break; */
  247.             translate(-x_sep,0,0);
  248.  
  249.                     /* weird logic to skip polygons */
  250.             if (lightWeight) {
  251.                 if (skipPolyCounter < lightWeightCount) {
  252.                 skipPolyCounter++;
  253.                 continue;
  254.             }
  255.             else {
  256.                 skipPolyCounter = 0;
  257.             }
  258.                     } 
  259. #ifdef OPTI
  260.             texbind(TX_TEXTURE_0,400+i+1);
  261. #endif
  262.             cpack(cpackValue);
  263.             if (doLighting) n3f(yzNorm);
  264.             bgnpolygon();
  265. #ifndef OPTI
  266. t0[1] = t1[1] = (float)i/(float)width;
  267. t2[1] = t3[1] = (float)(i+1)/(float)width;
  268. #endif
  269.             t2f(t0);
  270.             v3f(yz_v0);
  271.             t2f(t1);
  272.             v3f(yz_v1);
  273.             t2f(t2);
  274.             v3f(yz_v2);
  275.             t2f(t3);
  276.             v3f(yz_v3);
  277.             endpolygon();
  278.         }
  279.         break;
  280.         case _ZX :
  281.         skipPolyCounter = 0;
  282.         translate(0,y_sep*height/2.0,0);
  283. #ifndef OPTI
  284. if (lastSet != viewAxis) texbind(TX_TEXTURE_0,800);
  285. #endif
  286.         for (i=0;i<height;i++) {
  287.                     /* if (degenMode && qtest()) break; */
  288.             translate(0,-y_sep,0);
  289.  
  290.                     /* weird logic to skip polygons */
  291.             if (lightWeight) {
  292.                 if (skipPolyCounter < lightWeightCount) {
  293.                 skipPolyCounter++;
  294.                 continue;
  295.             }
  296.             else {
  297.                 skipPolyCounter = 0;
  298.             }
  299.                     } 
  300. #ifdef OPTI
  301.             texbind(TX_TEXTURE_0,800+i+1);
  302. #endif
  303.             cpack(cpackValue);
  304.             if (doLighting) n3f(zxNorm);
  305.             bgnpolygon();
  306. #ifndef OPTI
  307. t0[1] = t1[1] = (float)i/(float)height;
  308. t2[1] = t3[1] = (float)(i+1)/(float)height;
  309. #endif
  310.             t2f(t0);
  311.             v3f(zx_v0);
  312.             t2f(t1);
  313.             v3f(zx_v1);
  314.             t2f(t2);
  315.             v3f(zx_v2);
  316.             t2f(t3);
  317.             v3f(zx_v3);
  318.             endpolygon();
  319.         }
  320.         break;
  321.         case _MXY :
  322.         skipPolyCounter = 0;
  323.         translate(0,0,-z_sep*depth/2.0);
  324. #ifndef OPTI
  325. if (lastSet != viewAxis) texbind(TX_TEXTURE_0,100);
  326. #endif
  327.         for (i=depth-1;i>=0;i--) {       
  328.                     /* if (degenMode && qtest()) break; */
  329.             translate(0,0,z_sep);
  330.  
  331.                     /* weird logic to skip polygons */
  332.             if (lightWeight) {
  333.                 if (skipPolyCounter < lightWeightCount) {
  334.                 skipPolyCounter++;
  335.                 continue;
  336.             }
  337.             else {
  338.                 skipPolyCounter = 0;
  339.             }
  340.                     } 
  341. #ifdef OPTI
  342.             texbind(TX_TEXTURE_0,i+1);
  343. #endif
  344.             cpack(cpackValue);
  345.             if (doLighting) n3f(mxyNorm);
  346.             bgnpolygon();
  347. #ifndef OPTI
  348. t0[1] = t1[1] = (float)i/(float)depth;
  349. t2[1] = t3[1] = (float)(i+1)/(float)depth;
  350. #endif
  351.             t2f(t0);
  352.             v3f(xy_v0);
  353.             t2f(t1);
  354.             v3f(xy_v1);
  355.             t2f(t2);
  356.             v3f(xy_v2);
  357.             t2f(t3);
  358.             v3f(xy_v3);
  359.             endpolygon();
  360.         }
  361.         break;
  362.         case _MYZ :
  363.         skipPolyCounter = 0;
  364.         translate(-x_sep*width/2.0,0,0);
  365. #ifndef OPTI
  366. if (lastSet != viewAxis) texbind(TX_TEXTURE_0,400);
  367. #endif
  368.         for (i=width-1;i>=0;i--) {
  369.                     /* if (degenMode && qtest()) break; */
  370.             translate(x_sep,0,0);
  371.  
  372.                     /* weird logic to skip polygons */
  373.             if (lightWeight) {
  374.                 if (skipPolyCounter < lightWeightCount) {
  375.                 skipPolyCounter++;
  376.                 continue;
  377.             }
  378.             else {
  379.                 skipPolyCounter = 0;
  380.             }
  381.                     } 
  382. #ifdef OPTI
  383.             texbind(TX_TEXTURE_0,400+i+1);
  384. #endif
  385.             cpack(cpackValue);
  386.             if (doLighting) n3f(myzNorm);
  387.             bgnpolygon();
  388. #ifndef OPTI
  389. t0[1] = t1[1] = (float)i/(float)width;
  390. t2[1] = t3[1] = (float)(i+1)/(float)width;
  391. #endif
  392.             t2f(t0);
  393.             v3f(yz_v0);
  394.             t2f(t1);
  395.             v3f(yz_v1);
  396.             t2f(t2);
  397.             v3f(yz_v2);
  398.             t2f(t3);
  399.             v3f(yz_v3);
  400.             endpolygon();
  401.         }
  402.         break;
  403.         case _MZX :
  404.         skipPolyCounter = 0;
  405.         translate(0,-y_sep*height/2.0,0);
  406. #ifndef OPTI
  407. if (lastSet != viewAxis) texbind(TX_TEXTURE_0,800);
  408. #endif
  409.         for (i=height-1;i>=0;i--) {
  410.                     /* if (degenMode && qtest()) break; */
  411.             translate(0,y_sep,0);
  412.                     /* weird logic to skip polygons */
  413.             if (lightWeight) {
  414.                 if (skipPolyCounter < lightWeightCount) {
  415.                 skipPolyCounter++;
  416.                 continue;
  417.             }
  418.             else {
  419.                 skipPolyCounter = 0;
  420.             }
  421.                     } 
  422. #ifdef OPTI
  423.             texbind(TX_TEXTURE_0,800+i+1);
  424. #endif
  425.             cpack(cpackValue);
  426.             if (doLighting) n3f(mzxNorm);
  427.             bgnpolygon();
  428. #ifndef OPTI
  429. t0[1] = t1[1] = (float)i/(float)height;
  430. t2[1] = t3[1] = (float)(i+1)/(float)height;
  431. #endif
  432.             t2f(t0);
  433.             v3f(zx_v0);
  434.             t2f(t1);
  435.             v3f(zx_v1);
  436.             t2f(t2);
  437.             v3f(zx_v2);
  438.             t2f(t3);
  439.             v3f(zx_v3);
  440.             endpolygon();
  441.         }
  442.         break;
  443.         }
  444.             tevbind(TV_ENV0,0);
  445.     }
  446.  
  447. /* XXX3 */
  448.  
  449.     else if (textureMode == TEXTURE_3D) {
  450.             tevbind(TV_ENV0,1);
  451.         switch(viewAxis) {
  452.         case _XY :    /* 3D */
  453.         skipPolyCounter = 0;
  454.  
  455.                 tcoord[0][0] = 0.0; tcoord[0][1] = 0.0;
  456.                 tcoord[1][0] = 1.0; tcoord[1][1] = 0.0;
  457.                 tcoord[2][0] = 1.0; tcoord[2][1] = 1.0;
  458.                 tcoord[3][0] = 0.0; tcoord[3][1] = 1.0;
  459.  
  460.                 translate(0,0,z_sep*depth/2.0);
  461.  
  462.                 for (i=0;i<depth;i++) {
  463.                     /* if (degenMode && qtest()) break; */
  464.     
  465.                     tcoord[0][2] = tcoord[1][2] =
  466.                     tcoord[2][2] = tcoord[3][2] = (float)i/(float)depth;
  467.  
  468.                     translate(0,0,-z_sep);
  469.  
  470.                     /* weird logic to skip polygons */
  471.             if (lightWeight) {
  472.                 if (skipPolyCounter < lightWeightCount) {
  473.                 skipPolyCounter++;
  474.                 continue;
  475.             }
  476.             else {
  477.                 skipPolyCounter = 0;
  478.             }
  479.                     } 
  480.                     cpack(cpackValue);
  481.                     if (doLighting) n3f(xyNorm);
  482.                     bgnpolygon();
  483.                     t3f(tcoord[0]);
  484.                     v3f(xy_v0);
  485.                     t3f(tcoord[1]);
  486.                     v3f(xy_v1);
  487.                     t3f(tcoord[2]);
  488.                     v3f(xy_v2);
  489.                     t3f(tcoord[3]);
  490.                     v3f(xy_v3);
  491.                     endpolygon();
  492.                 }
  493.                 break;
  494.  
  495.         case _YZ :    /* 3D */
  496.         skipPolyCounter = 0;
  497.  
  498.                 tcoord[0][1] = 0.0; tcoord[0][2] = 1.0;
  499.                 tcoord[1][1] = 1.0; tcoord[1][2] = 1.0;
  500.                 tcoord[2][1] = 1.0; tcoord[2][2] = 0.0;
  501.                 tcoord[3][1] = 0.0; tcoord[3][2] = 0.0;
  502.  
  503.                 translate(x_sep*width/2.0,0,0);
  504.                 for (i=width-1;i>=0;i--) {
  505.                     /* if (degenMode && qtest()) break; */
  506.  
  507.                     tcoord[0][0] = tcoord[1][0] =
  508.                     tcoord[2][0] = tcoord[3][0] = (float)i/(float)(width-1);
  509.  
  510.                     translate(-x_sep,0,0);
  511.  
  512.                     /* weird logic to skip polygons */
  513.             if (lightWeight) {
  514.                 if (skipPolyCounter < lightWeightCount) {
  515.                 skipPolyCounter++;
  516.                 continue;
  517.             }
  518.             else {
  519.                 skipPolyCounter = 0;
  520.             }
  521.                     } 
  522.  
  523.                     cpack(cpackValue);
  524.                     if (doLighting) n3f(yzNorm);
  525.  
  526.                     bgnpolygon();
  527.                     t3f(tcoord[0]);
  528.                     v3f(yz_v0);
  529.                     t3f(tcoord[1]);
  530.                     v3f(yz_v1);
  531.                     t3f(tcoord[2]);
  532.                     v3f(yz_v2);
  533.                     t3f(tcoord[3]);
  534.                     v3f(yz_v3);
  535.                     endpolygon();
  536.                 }
  537.                 break;
  538.         case _ZX :    /* 3D */
  539.         skipPolyCounter = 0;
  540.  
  541.                 tcoord[0][2] = 0.0; tcoord[0][0] = 0.0;
  542.                 tcoord[1][2] = 1.0; tcoord[1][0] = 0.0;
  543.                 tcoord[2][2] = 1.0; tcoord[2][0] = 1.0;
  544.                 tcoord[3][2] = 0.0; tcoord[3][0] = 1.0;
  545.  
  546.                 translate(0,y_sep*height/2.0,0);
  547.                 for (i=height-1;i>=0;i--) {
  548.                     /* if (degenMode && qtest()) break; */
  549.  
  550.                     tcoord[0][1] = tcoord[1][1] =
  551.                     tcoord[2][1] = tcoord[3][1] = (float)i/(float)(height-1);
  552.  
  553.                     translate(0,-y_sep,0);
  554.  
  555.                     /* weird logic to skip polygons */
  556.             if (lightWeight) {
  557.                 if (skipPolyCounter < lightWeightCount) {
  558.                 skipPolyCounter++;
  559.                 continue;
  560.             }
  561.             else {
  562.                 skipPolyCounter = 0;
  563.             }
  564.                     } 
  565.                     cpack(cpackValue);
  566.                     if (doLighting) n3f(zxNorm);
  567.                     bgnpolygon();
  568.                     t3f(tcoord[0]);
  569.                     v3f(zx_v0);
  570.                     t3f(tcoord[1]);
  571.                     v3f(zx_v1);
  572.                     t3f(tcoord[2]);
  573.                     v3f(zx_v2);
  574.                     t3f(tcoord[3]);
  575.                     v3f(zx_v3);
  576.                     endpolygon();
  577.                 }
  578.  
  579.                 break;
  580.         case _MXY :    /* 3D */
  581.         skipPolyCounter = 0;
  582.  
  583.                 tcoord[0][0] = 0.0; tcoord[0][1] = 0.0;
  584.                 tcoord[1][0] = 1.0; tcoord[1][1] = 0.0;
  585.                 tcoord[2][0] = 1.0; tcoord[2][1] = 1.0;
  586.                 tcoord[3][0] = 0.0; tcoord[3][1] = 1.0;
  587.  
  588.                 translate(0,0,-z_sep*depth/2.0);
  589.                 for (i=depth-1;i>=0;i--) {
  590.                     /* if (degenMode && qtest()) break; */
  591.     
  592.                     tcoord[0][2] = tcoord[1][2] =
  593.                     tcoord[2][2] = tcoord[3][2] = (float)i/(float)(depth-1);
  594.  
  595.                     translate(0,0,z_sep);
  596.                     /* weird logic to skip polygons */
  597.             if (lightWeight) {
  598.                 if (skipPolyCounter < lightWeightCount) {
  599.                 skipPolyCounter++;
  600.                 continue;
  601.             }
  602.             else {
  603.                 skipPolyCounter = 0;
  604.             }
  605.                     } 
  606.                     cpack(cpackValue);
  607.                     if (doLighting) n3f(mxyNorm);
  608.                     bgnpolygon();
  609.                     t3f(tcoord[0]);
  610.                     v3f(xy_v0);
  611.                     t3f(tcoord[1]);
  612.                     v3f(xy_v1);
  613.                     t3f(tcoord[2]);
  614.                     v3f(xy_v2);
  615.                     t3f(tcoord[3]);
  616.                     v3f(xy_v3);
  617.                     endpolygon();
  618.                 }
  619.                 break;
  620.  
  621.         case _MYZ :    /* 3D */
  622.         skipPolyCounter = 0;
  623.  
  624.                 tcoord[0][1] = 0.0; tcoord[0][2] = 1.0;
  625.                 tcoord[1][1] = 1.0; tcoord[1][2] = 1.0;
  626.                 tcoord[2][1] = 1.0; tcoord[2][2] = 0.0;
  627.                 tcoord[3][1] = 0.0; tcoord[3][2] = 0.0;
  628.  
  629.                 translate(-x_sep*width/2.0,0,0);
  630.                 for (i=0;i<width;i++) {
  631.                     /* if (degenMode && qtest()) break; */
  632.  
  633.                     tcoord[0][0] = tcoord[1][0] =
  634.                     tcoord[2][0] = tcoord[3][0] = (float)i/(float)(width);
  635.                     translate(x_sep,0,0);
  636.  
  637.                     /* weird logic to skip polygons */
  638.             if (lightWeight) {
  639.                 if (skipPolyCounter < lightWeightCount) {
  640.                 skipPolyCounter++;
  641.                 continue;
  642.             }
  643.             else {
  644.                 skipPolyCounter = 0;
  645.             }
  646.                     } 
  647.                     cpack(cpackValue);
  648.                     if (doLighting) n3f(myzNorm);
  649.                     bgnpolygon();
  650.                     t3f(tcoord[0]);
  651.                     v3f(yz_v3);
  652.                     t3f(tcoord[1]);
  653.                     v3f(yz_v2);
  654.                     t3f(tcoord[2]);
  655.                     v3f(yz_v1);
  656.                     t3f(tcoord[3]);
  657.                     v3f(yz_v0);
  658.                     endpolygon();
  659.                 }
  660.  
  661.                 break;
  662.         case _MZX :    /* 3D */
  663.         skipPolyCounter = 0;
  664.  
  665.                 tcoord[0][2] = 0.0; tcoord[0][0] = 1.0;
  666.                 tcoord[1][2] = 1.0; tcoord[1][0] = 1.0;
  667.                 tcoord[2][2] = 1.0; tcoord[2][0] = 0.0;
  668.                 tcoord[3][2] = 0.0; tcoord[3][0] = 0.0;
  669.  
  670.                 translate(0,-y_sep*height/2.0,0);
  671.                 for (i=0;i<height;i++) {
  672.                     if (degenMode && qtest()) {
  673.                         break;
  674.                     }
  675.                     tcoord[0][1] = tcoord[1][1] =
  676.                     tcoord[2][1] = tcoord[3][1] = (float)i/(float)(height);
  677.                     translate(0,y_sep,0);
  678.  
  679.                     /* weird logic to skip polygons */
  680.             if (lightWeight) {
  681.                 if (skipPolyCounter < lightWeightCount) {
  682.                 skipPolyCounter++;
  683.                 continue;
  684.             }
  685.             else {
  686.                 skipPolyCounter = 0;
  687.             }
  688.                     } 
  689.  
  690.                     cpack(cpackValue);
  691.                     if (doLighting) n3f(mzxNorm);
  692.                     bgnpolygon();
  693.                     t3f(tcoord[0]);
  694.                     v3f(zx_v3);
  695.                     t3f(tcoord[1]);
  696.                     v3f(zx_v2);
  697.                     t3f(tcoord[2]);
  698.                     v3f(zx_v1);
  699.                     t3f(tcoord[3]);
  700.                     v3f(zx_v0);
  701.                     endpolygon();
  702.                 }
  703.  
  704.                 break;
  705.         } /* sw viewAxis in 3d */
  706. /*
  707.             texbind(TX_TEXTURE_0,0);
  708. */
  709.             tevbind(TV_ENV0,0);
  710.     } /* 3D texture */
  711.  
  712.     if (!dontBlend)
  713.         blendfunction( BF_ONE, BF_ZERO );
  714.  
  715.         popmatrix();
  716.         if (drawAxis)  drawAxes();
  717.  
  718.         if (doClipping && displayClipPlanes) {
  719.             for (i=0;i<6;i++) {
  720.                 if (clip[i].onOff==1) {
  721.                     drawClippingPlane(i);
  722.                 }
  723.             }
  724.         }
  725.  
  726.     break;  /* case TEXTURE */
  727.     } /* switch drawmode */
  728.  
  729.     lastSet = viewAxis;
  730.  
  731.     if (doubleBuffer) swapbuffers();
  732.  
  733.     if (doLighting) {
  734.         lmbind(LMODEL,0);
  735.     }
  736.     if (fog) {
  737.     fogvertex(FG_OFF,0);
  738.     }
  739.     if (doClipping) {
  740.         for (i=0;i<6;i++) {
  741.             if (clip[i].onOff == 1) {
  742.                 clipplane(i,CP_OFF,0);
  743.             }
  744.         }
  745.     }
  746. }
  747.  
  748. void
  749. drawAxes()
  750. {
  751.     RGBcolor(0,255,0);
  752.     move(0.0,0.0,0.0);
  753.     draw(bmaxx,0.0,0.0);
  754.     cmov(0.95*bmaxx,0.0,0.0);
  755.     if (isFacing(1.0,0.0,0.0))
  756.     charstr("x");
  757.  
  758.     RGBcolor(0,0,255);
  759.     move(0.0,0.0,0.0);
  760.     draw(0.0,bmaxy,0.0);
  761.     cmov(0.0,0.95*bmaxy,0.0);
  762.     if (isFacing(0.0,1.0,0.0))
  763.     charstr("y");
  764.  
  765.     RGBcolor(255,0,0);
  766.     move(0.0,0.0,0.0);
  767.     draw(0.0,0.0,bmaxz);
  768.     cmov(0.0,0.0,0.95*bmaxz);
  769.     if (isFacing(0.0,0.0,1.0))
  770.     charstr("z");
  771. }
  772.  
  773. int
  774. isFacing(nx,ny,nz)
  775. float nx,ny,nz;
  776. {
  777.     float viewDir[3],planeDir[3];
  778.     Matrix foo,vinv;
  779.  
  780.     getmatrix(foo);
  781.     invert4d( foo, vinv );
  782.  
  783.     viewDir[0] = -vinv[2][0];
  784.     viewDir[1] = -vinv[2][1];
  785.     viewDir[2] = -vinv[2][2];
  786.  
  787.     normalize(&viewDir[0],&viewDir[1],&viewDir[2]);
  788.  
  789.     planeDir[0] = nx;
  790.     planeDir[1] = ny;
  791.     planeDir[2] = nz;
  792.  
  793.     normalize(&planeDir[0],&planeDir[1],&planeDir[2]);
  794.  
  795.     if ((viewDir[0]*planeDir[0])+
  796.         (viewDir[1]*planeDir[1])+
  797.         (viewDir[2]*planeDir[2]) <= 0)  return(1);
  798.     else 
  799.         return(0);
  800. }
  801.  
  802. int
  803. optimumSet()
  804. {
  805.     float viewDir[3];
  806.     Matrix foo,vinv;
  807.     float maxf,xy,yz,zx;
  808.     int im;
  809.  
  810.     getmatrix(foo);
  811.     invert4d( foo, vinv );
  812.  
  813.     viewDir[0] = -vinv[2][0];
  814.     viewDir[1] = -vinv[2][1];
  815.     viewDir[2] = -vinv[2][2];
  816.  
  817.     normalize(&viewDir[0],&viewDir[1],&viewDir[2]);
  818.  
  819.     xy = viewDir[2];    /* simplified because 0*xx + 0*yy + 1*zz */
  820.     yz = viewDir[0];
  821.     zx = viewDir[1];
  822.  
  823.     maxf = ABS(xy);
  824.     im   = (xy < 0.0) ? _XY : _MXY;
  825.  
  826.     if (maxf <= ABS(yz)) {
  827.     maxf = ABS(yz);
  828.         im   = (yz < 0.0) ? _YZ : _MYZ;
  829.     }
  830.  
  831.     if (maxf <= ABS(zx)) {
  832.     maxf = ABS(zx);
  833.         im   = (zx < 0.0) ? _ZX : _MZX;
  834.     }
  835.  
  836.     if (lockAView != 0) {
  837.         im = (im < 0) ? -lockAView : lockAView;
  838.     }
  839.  
  840.     return(im); 
  841. }
  842.  
  843. void
  844. drawClippingPlane(n)
  845. int n;
  846. {
  847.     static float square[4][3];
  848.     static float norm[3];
  849.  
  850.     switch(n) {
  851.     case 0 :
  852.         RGBcolor(255,0,0);
  853.         square[0][_X] = clip[n].xyz[_X];
  854.         square[0][_Y] = bminy;
  855.         square[0][_Z] = bminz;
  856.  
  857.         square[1][_X] = clip[n].xyz[_X];
  858.         square[1][_Y] = bminy;
  859.         square[1][_Z] = bmaxz;
  860.  
  861.         square[2][_X] = clip[n].xyz[_X];
  862.         square[2][_Y] = bmaxy;
  863.         square[2][_Z] = bmaxz;
  864.  
  865.         square[3][_X] = clip[n].xyz[_X];
  866.         square[3][_Y] = bmaxy;
  867.         square[3][_Z] = bminz;
  868.         norm[0] = -1.0;
  869.         norm[1] = 0.0;
  870.         norm[2] = 0.0;
  871.  
  872.         break;
  873.     case 1 :
  874.         RGBcolor(0,255,0);
  875.         square[0][_X] = clip[n].xyz[_X];
  876.         square[0][_Y] = bminy;
  877.         square[0][_Z] = bminz;
  878.  
  879.         square[1][_X] = clip[n].xyz[_X];
  880.         square[1][_Y] = bmaxy;
  881.         square[1][_Z] = bminz;
  882.  
  883.         square[2][_X] = clip[n].xyz[_X];
  884.         square[2][_Y] = bmaxy;
  885.         square[2][_Z] = bmaxz;
  886.  
  887.         square[3][_X] = clip[n].xyz[_X];
  888.         square[3][_Y] = bminy;
  889.         square[3][_Z] = bmaxz;
  890.         norm[0] = 1.0;
  891.         norm[1] = 0.0;
  892.         norm[2] = 0.0;
  893.  
  894.         break;
  895.     case 2 :
  896.         RGBcolor(255,255,0);
  897.         square[0][_X] = bminx;
  898.         square[0][_Y] = clip[n].xyz[_Y];
  899.         square[0][_Z] = bminz;
  900.  
  901.         square[1][_X] = bminx;
  902.         square[1][_Y] = clip[n].xyz[_Y];
  903.         square[1][_Z] = bmaxz;
  904.  
  905.         square[2][_X] = bmaxx;
  906.         square[2][_Y] = clip[n].xyz[_Y];
  907.         square[2][_Z] = bmaxz;
  908.  
  909.         square[3][_X] = bmaxx;
  910.         square[3][_Y] = clip[n].xyz[_Y];
  911.         square[3][_Z] = bminz;
  912.         norm[0] = 0.0;
  913.         norm[1] = -1.0;
  914.         norm[2] = 0.0;
  915.  
  916.         break;
  917.     case 3 :
  918.         RGBcolor(0,0,255);
  919.         square[0][_X] = bminx;
  920.         square[0][_Y] = clip[n].xyz[_Y];
  921.         square[0][_Z] = bminz;
  922.  
  923.         square[1][_X] = bmaxx;
  924.         square[1][_Y] = clip[n].xyz[_Y];
  925.         square[1][_Z] = bminz;
  926.  
  927.         square[2][_X] = bmaxx;
  928.         square[2][_Y] = clip[n].xyz[_Y];
  929.         square[2][_Z] = bmaxz;
  930.  
  931.         square[3][_X] = bminx;
  932.         square[3][_Y] = clip[n].xyz[_Y];
  933.         square[3][_Z] = bmaxz;
  934.         norm[0] = 0.0;
  935.         norm[1] = 1.0;
  936.         norm[2] = 0.0;
  937.  
  938.         break;
  939.     case 4 :
  940.         RGBcolor(255,0,255);
  941.         square[0][_X] = bminx;
  942.         square[0][_Y] = bminy;
  943.         square[0][_Z] = clip[n].xyz[_Z];
  944.  
  945.         square[1][_X] = bminx;
  946.         square[1][_Y] = bmaxy;
  947.         square[1][_Z] = clip[n].xyz[_Z];
  948.  
  949.         square[2][_X] = bmaxx;
  950.         square[2][_Y] = bmaxy;
  951.         square[2][_Z] = clip[n].xyz[_Z];
  952.  
  953.         square[3][_X] = bmaxx;
  954.         square[3][_Y] = bminy;
  955.         square[3][_Z] = clip[n].xyz[_Z];
  956.         norm[0] = 0.0;
  957.         norm[1] = 0.0;
  958.         norm[2] = -1.0;
  959.  
  960.         break;
  961.     case 5 :
  962.         RGBcolor(0,255,255);
  963.         square[0][_X] = bminx;
  964.         square[0][_Y] = bminy;
  965.         square[0][_Z] = clip[n].xyz[_Z];
  966.  
  967.         square[1][_X] = bmaxx;
  968.         square[1][_Y] = bminy;
  969.         square[1][_Z] = clip[n].xyz[_Z];
  970.  
  971.         square[2][_X] = bmaxx;
  972.         square[2][_Y] = bmaxy;
  973.         square[2][_Z] = clip[n].xyz[_Z];
  974.  
  975.         square[3][_X] = bminx;
  976.         square[3][_Y] = bmaxy;
  977.         square[3][_Z] = clip[n].xyz[_Z];
  978.         norm[0] = 0.0;
  979.         norm[1] = 0.0;
  980.         norm[2] = 1.0;
  981.  
  982.         break;
  983.     }
  984.  
  985.     bgnclosedline();
  986.     v3f(square[0]);
  987.     v3f(square[1]);
  988.     v3f(square[2]);
  989.     v3f(square[3]);
  990.     endclosedline();
  991. }
  992.  
  993. unsigned long 
  994. packc(a,b,g,r)
  995. {
  996.     unsigned long retval;
  997.     retval = 0;
  998.     retval = (a<<24 | b<<16 | g<<8 | r);
  999.     return(retval);
  1000. }
  1001.